<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="css/mui.min.css">
		<style>
			[v-cloak] {
				display: none;
			}
			
			html,
			body {
				width: 100%;
				height: 100%
			}
			
			body {
				background: url(images/login-bg.png);
				background-repeat: no-repeat;
				background-size: 100% 100%;
			}
			
			.content {
				display: table;
				height: 100%;
				width: 100%;
			}
			
			.login-btn {
				display: table-cell;
				vertical-align: middle;
				text-align: center;
			}
			
			.login-btn button {
				border: 0px;
				background-color: #23be2d;
				color: #ffffff;
				height: 40px;
				outline: none;
				border-radius: 20px;
				margin: 0px;
				max-width: 334px;
			}
		</style>
	</head>

	<body>
		<div class="content" id="bizVue" v-cloak>
			<div class="login-btn">
				<div>
					<button type="button" style="width: 80%;" @click="validateForm">微信登陆</button>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/vue.min.js"></script>
		<script type="text/javascript" src="js/validator.min.js"></script>
		<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript" src="js/public.js"></script>
		<script>
			mui.init();
			var vm = new Vue({
				el: '#bizVue',
				data: {
					form: {}
				},
				mounted: function() {
					mui.plusReady(function() {
						//读取本地存储，检查是否为首次启动
						var showGuide = plus.storage.getItem("lauchFlag");
						if(showGuide) {
							vm.startup();
						} else {
							//显示启动导航
							mui.openWindow({
								id: 'guide',
								url: 'login/guide.html',
								styles: {
									popGesture: "none"
								},
								show: {
									aniShow: 'none'
								},
								waiting: {
									autoShow: false
								}
							});
							//延迟的原因：优先打开启动导航页面，避免资源争夺
							setTimeout(function() {
								vm.startup();
							}, 100);
						}
					});
				},
				methods: {
					startup: function() {
						//config init
						mui.postJSON(ConfigInit, {}, function(result) {
							mConsole.log("获取配置信息：" + JSON.stringify(result));
							if(result.code == 0) {
								plus.storage.setItem("config", JSON.stringify(result));
							}
						});

						//resetToken
						mui.postJSON(Rettoken, {}, function(result) {
							mConsole.log("重置tooken：" + JSON.stringify(result));
							if(result.code == 0) {
								plus.storage.setItem("tooken", result.token);
								mui.preload({
									url: "main/main.html",
									id: "main"
								});
								vm.checkPreload();
								return;
							}
							plus.navigator.closeSplashscreen(); //关闭启动界面
						});

						//检查更新
						plus.io.resolveLocalFileSystemURL("_doc/update/", function(entry) {
							if(entry.isDirectory) {
								//删除历史下载版本
								entry.removeRecursively(function() {
									vm.checkUpdate();
								});
							}
						}, function(e) {
							vm.checkUpdate();
						});
					},
					login: function() {
						plus.nativeUI.showWaiting('正在登录', {
							width: '100px',
							height: '100px'
						});
						mui.postJSON(LoginUrl, vm.form, function(result) {
							plus.nativeUI.closeWaiting();
							if(result.code == 0) {
								plus.storage.setItem("tooken", result.token);
								mui.openWindow("main/main.html", "main");
								return;
							}
							mymui(result.msg);
						});
					},
					validateForm: function() {
						plus.oauth.getServices(function(services) {
							if(services.length > 0) {
								var weixin = null;
								for(var i = 0; i < services.length; i++) {
									if(services[i].id == 'weixin') {
										weixin = services[i];
										break;
									}
								}
								if(weixin) {
									weixin.login(function(e) {
										vm.form.code = e.target.userInfo.openid;
										vm.form.token = e.target.authResult.refresh_token;
										vm.login();
										weixin.logout();
									}, function(e) {
										mymui("登录失败");
									});
								} else {
									mymui("请确认手机已安装微信客户端");
								}
							} else {
								mymui("services is null");
							}
						}, function(e) {
							mymui(e.message);
						});
					},
					checkPreload: function() {
						var array = plus.webview.all();
						if(array) {
							for(var i = 0, len = array.length; i < len; i++) {
								if(array[i].getURL().endWith("main.html")) {
									plus.webview.getWebviewById("main").show();
									plus.navigator.closeSplashscreen(); //关闭启动界面
									return;
								}
							}
						}
						setTimeout(vm.checkPreload, 500);
					},
					checkUpdate: function() {
						//检查更新
						plus.runtime.getProperty(plus.runtime.appid, function(inf) {
							mConsole.log("当前应用版本：" + inf.version);
							mui.postParam(CheckUpdate, {
								curVersion: inf.version
							}, function(result) {
								if(result.code == 0) {
									if(result.update) {
										mui.alert("发现有新的版本", "更新提示", "现在更新", function() {
											mymui("正在下载更新包");
											plus.downloader.createDownload(result.url, {
												filename: "_doc/update/"
											}, function(d, status) {
												if(status == 200) {
													mConsole.log("下载更新包成功：" + d.filename);
													mymui("正在安装更新包,请稍等...");
													plus.runtime.install(d.filename, {
														force: true
													}, function() {
														mymui("安装更新成功！");
													}, function(e) {
														mymui("安装更新包失败[" + e.code + "]：" + e.message);
													});
												} else {
													mymui("下载更新包失败！");
												}
											}).start();
										});
									}
								}
							});
						});
					}
				}
			});
		</script>
	</body>

</html>